package xx.book.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import xx.book.entity.Book;
import xx.book.service.BookService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Book)表控制层
 *
 * @author halo-king
 * @since 2023-06-18 16:23:15
 */
@Api(value = "图书接口")
@RestController
@RequestMapping("book")
public class BookController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private BookService bookService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param book 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAllByPage")
    public R selectAllByPage(Page<Book> page, Book book) {
        return success(this.bookService.page(page, new QueryWrapper<Book>(book).orderByDesc("create_time")));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param bookId 主键
     * @return 单条数据
     */
    @GetMapping("/queryBookDetail")
    public R selectOne(Integer bookId) {
        return success(this.bookService.getById(bookId));
    }

    /**
     * 新增\修改数据
     *
     * @param book 实体对象
     * @return 新增结果
     */
    @PostMapping("saveOrUpdate")
    public R insert(@RequestBody Book book) {
        if(book.getId() == null){
            return success(this.bookService.save(book));
        }else{
            return success(this.bookService.updateById(book));
        }

    }


    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Long> idList) {
        return success(this.bookService.removeByIds(idList));
    }
}
